<script lang="ts" setup>
import type { VxeTableGridOptions } from '#/adapter/vxe-table';
import type { HandCardAssetApi } from '#/api/spa/handcard/asset';

import { Page, useVbenModal } from '@vben/common-ui';
import { message,Tabs } from 'ant-design-vue';
import Form from './modules/form.vue';

import HandCardUsageList from './modules/hand-card-usage-list.vue'
import HandCardActionLogList from './modules/hand-card-action-log-list.vue'

import { ref, computed } from 'vue';
import { $t } from '#/locales';
import { ACTION_ICON, TableAction, useVbenVxeGrid } from '#/adapter/vxe-table';
import { getHandCardAssetPage, deleteHandCardAsset, exportHandCardAsset } from '#/api/spa/handcard/asset';
import { downloadFileFromBlobPart, isEmpty } from '@vben/utils';

import { useGridColumns, useGridFormSchema } from './data';

/** 子表的列表 */
const subTabsName = ref('handCardUsage')
const selectHandCardAsset = ref<HandCardAssetApi.HandCardAsset>();

const [FormModal, formModalApi] = useVbenModal({
  connectedComponent: Form,
  destroyOnClose: true,
});


/** 刷新表格 */
function onRefresh() {
  gridApi.query();
}

/** 创建手牌资产 */
function handleCreate() {
  formModalApi.setData({}).open();
}

/** 编辑手牌资产 */
function handleEdit(row: HandCardAssetApi.HandCardAsset) {
  formModalApi.setData(row).open();
}


/** 删除手牌资产 */
async function handleDelete(row: HandCardAssetApi.HandCardAsset) {
  const hideLoading = message.loading({
    content: $t('ui.actionMessage.deleting', [row.id]),
    key: 'action_key_msg',
  });
  try {
    await deleteHandCardAsset(row.id as number);
    message.success({
      content: $t('ui.actionMessage.deleteSuccess', [row.id]),
      key: 'action_key_msg',
    });
    onRefresh();
  } finally {
    hideLoading();
  }
}


/** 导出表格 */
async function handleExport() {
  const data = await exportHandCardAsset(await gridApi.formApi.getValues());
  downloadFileFromBlobPart({ fileName: '手牌资产.xls', source: data });
}

const [Grid, gridApi] = useVbenVxeGrid({
  formOptions: {
    schema: useGridFormSchema(),
  },
  gridOptions: {
    columns: useGridColumns(),
    height: '600px',
    pagerConfig: {
      enabled: true,
    },
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getHandCardAssetPage({
            pageNo: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
    rowConfig: {
      keyField: 'id',
      isHover: true,
      isCurrent: true,
    },
    toolbarConfig: {
      refresh: true,
      search: true,
    },
  } as VxeTableGridOptions<HandCardAssetApi.HandCardAsset>,
  gridEvents:{
    cellClick: ({ row }: { row: HandCardAssetApi.HandCardAsset}) => {
      selectHandCardAsset.value = row;
    },
  }
});
</script>

<template>
  <Page auto-content-height>
    <FormModal @success="onRefresh" />

    <div>
      <Grid table-title="手牌资产列表">
<!--        <template #toolbar-tools>-->
<!--          <TableAction-->
<!--            :actions="[-->
<!--            {-->
<!--              label: $t('ui.actionTitle.create', ['手牌资产']),-->
<!--              type: 'primary',-->
<!--              icon: ACTION_ICON.ADD,-->
<!--              auth: ['spa:hand-card-asset:create'],-->
<!--              onClick: handleCreate,-->
<!--            },-->
<!--            {-->
<!--              label: $t('ui.actionTitle.export'),-->
<!--              type: 'primary',-->
<!--              icon: ACTION_ICON.DOWNLOAD,-->
<!--              auth: ['spa:hand-card-asset:export'],-->
<!--              onClick: handleExport,-->
<!--            },-->
<!--          ]"-->
<!--          />-->
<!--        </template>-->
<!--        <template #actions="{ row }">-->
<!--          <TableAction-->
<!--            :actions="[-->
<!--            {-->
<!--              label: $t('common.edit'),-->
<!--              type: 'link',-->
<!--              icon: ACTION_ICON.EDIT,-->
<!--              auth: ['spa:hand-card-asset:update'],-->
<!--              onClick: handleEdit.bind(null, row),-->
<!--            },-->
<!--            {-->
<!--              label: $t('common.delete'),-->
<!--              type: 'link',-->
<!--              danger: true,-->
<!--              icon: ACTION_ICON.DELETE,-->
<!--              auth: ['spa:hand-card-asset:delete'],-->
<!--              popConfirm: {-->
<!--                title: $t('ui.actionMessage.deleteConfirm', [row.id]),-->
<!--                confirm: handleDelete.bind(null, row),-->
<!--              },-->
<!--            },-->
<!--          ]"-->
<!--          />-->
<!--        </template>-->
      </Grid>

      <!-- 子表的表单 -->
      <Tabs v-model:active-key="subTabsName" class="mt-2">
        <Tabs.TabPane key="handCardUsage" tab="手牌使用记录" force-render>
          <HandCardUsageList :card-id="selectHandCardAsset?.id" />
        </Tabs.TabPane>
        <Tabs.TabPane key="handCardActionLog" tab="手牌动作记录表（替换/撤销等）" force-render>
          <HandCardActionLogList :card-id="selectHandCardAsset?.id" />
        </Tabs.TabPane>
      </Tabs>
    </div>
  </Page>
</template>
